//
//  ApplyForOutHolidayViewController.m
//  AttendenceSys
//
//  Created by ld on 16/1/20.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ApplyForOutHolidayViewController.h"

@interface ApplyForOutHolidayViewController ()

@end

@implementation ApplyForOutHolidayViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.strNavTitle = [self getYearMonthDay:[NSDate date]];
    
//    _sDate = [self.strNavTitle stringByReplacingOccurrencesOfString:@"." withString:@"-"];
    
    //    [self setRightCallButtonWithImageName:@"ic_drop" callPressedImageName:@"ic_drop" rightButtonImageName:@"ic_drop" rightPressedImageName:@"ic_drop"];
    [self initView];
    [self setRightButtonWithImageName:@"ic_drop" bgImageName:@""];
    
    [self initView];
}

- (NSString*)getYearMonthDay:(NSDate*)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit
                          | NSMonthCalendarUnit
                          | NSDayCalendarUnit
                          | NSHourCalendarUnit
                          | NSMinuteCalendarUnit
                          | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:date];
    NSString* year = [NSString stringWithFormat:@"%ld", (long)[dateComponent year]];
    NSString* month = [NSString stringWithFormat:@"%ld", (long)[dateComponent month]];
    NSString* day = [NSString stringWithFormat:@"%ld", (long)[dateComponent day]];
    NSString* yearMonthDay = [NSString stringWithFormat:@"%@.%@.%@", year, month, day];
    
    return yearMonthDay;
}

- (void)initView
{
    self.calendarView.defaultDays = _seletedDays;
    [self.calendarView show];
    
    //next
    UIButton* nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    nextBtn.frame = CGRectMake(50,self.view.frame.size.height/2+ 80,[UIScreen mainScreen].bounds.size.width-50*2,50);
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    nextBtn.layer.cornerRadius = 4.5;
    [nextBtn addTarget:self action:@selector(nextBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextBtn.backgroundColor = RGB(252, 120, 61);
    [self.view addSubview:nextBtn];
}

- (void)nextBtn
{
    [_calendarView doneBtnClick:nil];
    NSString* select = [self showStr];
    if ((nil == select) || (0 == select.length))
    {
        [SVProgressHUD showErrorWithStatus:@"请选择要申请外出假"];
        return;
    }
    
    AppForClaLeaUpLoadPhoViewController* appForClassViewCtrl = [[AppForClaLeaUpLoadPhoViewController alloc] init];
    appForClassViewCtrl.bIsOutLeave = YES;
    appForClassViewCtrl.sSelectDate = select;
    appForClassViewCtrl.strDate = [self getYearMonthDay:[NSDate date]];
    appForClassViewCtrl.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:appForClassViewCtrl animated:YES];
    
    //[self ApplyForOutHoliday:select];
}

//外出假接口 http://211.65.216.54/uploadAction.do?type=2&leaveType=3&userCode=xiaohu&leaveDate=2016-02-26&termId=1 //考勤外出假的时候type=3 写死2 服务端有用 leaveType请假类型(1按天请假2请课假 3外出假) leaveReason请假理由courseId课程主键leaveDate请假日期weekDay星期几courseNumber第几节课startDate开始日期(yyyy-MM-dd)endDate结束日期(yyyy-MM-dd) termId//学期id

- (void)ApplyForOutHoliday:(NSString*)sDate
{
    NSString* refer = @"uploadAction.do?";
    NSString* prefix = kASHttpUrl(refer);
    
    //ASLoginUserInfo* loginInfo = [ASAppDelegate sharedAppDelegate].userInfo;
    
    NSString* courseUrl = [NSString stringWithFormat:@"%@type=%@&leaveType=%@&userCode=%@&leaveDate=%@&termId=%@",prefix, @"2", @"3", AS_APP_UserCode, sDate, @"1"];
    
    [SVProgressHUD showWithStatus:@"申请外出假..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    [networkingEngine GET:courseUrl parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        jsonRsp = [jsonRsp processDictionaryIsNSNull:jsonRsp];
        if (isSuccessRsp(jsonRsp))
        {
            [SVProgressHUD showErrorWithStatus:errorMsgInfoRsp(jsonRsp)];
            
            [SVProgressHUD dismiss];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSString *)showStr
{
    NSMutableString *str = [NSMutableString string];
    
    //[str appendString:@"已选择日期:\r\n"];
    //从小到大排序
    [self.seletedDays sortUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
        return [obj1 compare:obj2];
    }];
    
    for (int i=0; i < self.seletedDays.count; i++) {
        NSNumber *interval = [self.seletedDays objectAtIndex:i];
        NSString *partStr = [NSDate stringWithTimestamp:interval.doubleValue format:@"yyyy-MM-dd"];
        
        if ((i + 1) == self.seletedDays.count) {
            [str appendFormat:@"%@",partStr];
        }
        else
        {
            [str appendFormat:@"%@,",partStr];
        }
    }
    return str.copy;
}

- (ASCalendarView *)calendarView
{
    if (!_calendarView)
    {
        _calendarView = [[ASCalendarView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height/2)];
        [self.view addSubview:_calendarView];
        
        __weak typeof(self) weakSelf = self;
        _calendarView.complete = ^(NSArray *result) {
            if (result) {
                weakSelf.seletedDays = result.mutableCopy;
            }
        };
    }
    return _calendarView;
}


@end



